package com.wzh.yebServe.controller;

import com.wzh.yebServe.pojo.ResultBean;
import com.wzh.yebServe.pojo.User;
import com.wzh.yebServe.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author wzh
 * @create 2022/11/22 20:57
 * @Description:个人中心
 */
@RestController
public class UserInfoController {
    @Autowired
    private IUserService iUserService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    public ResultBean updateUser(@RequestBody User user, Authentication authentication){
        if (iUserService.updateById(user)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,null,authentication.getAuthorities()));
            return ResultBean.success("更新成功");
        }
        return ResultBean.error("更新失败");
    }

    @ApiOperation(value = "更新用户密码")
    @PutMapping("/admin/pass")
    public ResultBean updateUserPassword(@RequestBody Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
       Integer adminId = (Integer) info.get("adminId");
       return iUserService.updateUserPassword(oldPass,pass,adminId);
    }

}
